#include "music.h"

Music::Music()
    :isLike(false)
    ,isHistory(false)
{

}

Music::Music(QUrl url)
    :isLike(false)
    ,isHistory(false)
    ,musicUrl(url)
{
    // 读取传进来的url 解析出源数据
    // 歌曲名称 歌曲作者 歌曲专辑 歌曲时长

    musicId = QUuid::createUuid().toString();
    parseMediaMetaMusic();

}

void Music::setMusicName(const QString &musicName)
{
    this->musicName = musicName;
}

void Music::setMusicSinger(const QString &musicSinger)
{
    this->musicSinger = musicSinger;
}

void Music::setMusicAlbumn(const QString &musicAlbumn)
{
    this->musicAlbumn = musicAlbumn;
}

void Music::setMusicDuration(const qint64 duration)
{
    this->duration = duration;
}

void Music::setIsLike(bool isLike)
{
    this->isLike = isLike;
}

void Music::setIsHistory(bool isHistory)
{
    this->isHistory = isHistory;
}

void Music::setMusicUrl(QUrl musicUrl)
{
    this->musicUrl = musicUrl;
}

void Music::setMusicId(const QString &musicId)
{
    this->musicId = musicId;
}

QString Music::getMusicID() const
{
    return musicId;
}

QString Music::getMusicName() const
{
    return musicName;
}

QString Music::getMusicSinger() const
{
    return musicSinger;
}

QString Music::getMusicAlbumn() const
{
    return musicAlbumn;
}

qint64 Music::getMusicDuration() const
{
    return duration;
}

bool Music::getIsLike() const
{
    return isLike;
}

bool Music::getIsHistory() const
{
    return isHistory;
}

QUrl Music::getMusicUrl() const
{
    return musicUrl;
}

QString Music::getLrcFilePath() const
{
    QString lrcPath = musicUrl.toLocalFile();
    lrcPath.replace(".mp3",".lrc");
    lrcPath.replace(".flac",".lrc");
    lrcPath.replace(".mpga",".lrc");
    return lrcPath;
}

void Music::InsertMusicDB()
{
    qDebug() << "写入中";
    // 查询音乐是否在数据库中
    QSqlQuery query;
    query.prepare("SELECT EXISTS (SELECT 1 FROM MusicInfo WHERE musicId = ?)"); // 返回歌曲的ID
    query.addBindValue(musicId);
    if(!query.exec())
    {
        qDebug() << "查询失败" << query.lastError().text();
        return;
    }

    if(query.next())
    {
        bool isExists = query.value(0).toBool();
        // 存在则更新isLike isHistory属性
        if(isExists)
        {
            query.prepare("UPDATE MusicInfo SET isLike = ?, isHistory = ? WHERE musicId = ?");
            query.addBindValue(isLike?1:0);
            query.addBindValue(isHistory?1:0);
            query.addBindValue(musicId);
            if(!query.exec())
            {
                qDebug() << "更新失败" << query.lastError().text();
                return;
            }
            qDebug() << "更新成功" << musicId << ":" << musicName;
        }
        else
        {
            // 不存在直接将属性插入库中
             query.prepare("INSERT INTO MusicInfo(musicId, musicName,\
            musicSinger, albumName, musicUrl,\
             duration, isLike, isHistory)\
             VALUES(?,?,?,?,?,?,?,?)");
             query.addBindValue(musicId);
             query.addBindValue(musicName);
             query.addBindValue(musicSinger);
             query.addBindValue(musicAlbumn);
             query.addBindValue(musicUrl.toLocalFile());
             query.addBindValue(duration);
             query.addBindValue(isLike ? 1 : 0);
             query.addBindValue(isHistory? 1 : 0);
            if(!query.exec())
            {
                qDebug()<<musicId<<musicName<<musicSinger<<musicAlbumn<<musicUrl<<duration<<isLike<<isHistory;
                qDebug() << "插入失败" << query.lastError().text();
                return;
            }
            qDebug() << "插入成功" ;
        }
    }

}

void Music::parseMediaMetaMusic()
{
    // 创建媒体播放对象
    QMediaPlayer player;
    // 设置媒体源 依靠该方法解析元数据
    player.setMedia(musicUrl);
    // setmedia不会等待歌曲的音乐加载就会立马返回 因此需要确保加载完毕
    while(!player.isMetaDataAvailable())
    {
        // 返回false时候 进入循环等待加载
        QCoreApplication::processEvents();
    }
    // 加载完成后可以获取有效的元数据
    if(player.isMetaDataAvailable())
    {
        musicName = player.metaData("Title").toString();
        musicSinger = player.metaData("Author").toString();
        musicAlbumn = player.metaData("AlbumTitle").toString();
        duration = player.metaData("Duration").toLongLong();
        QString fileName = musicUrl.fileName();
        // 找“-”的位置
        int index = fileName.indexOf('-');
        // musicName为空
        if(musicName.isEmpty())
        {
            if(index != -1)
            {
                musicName = fileName.mid(0,index).trimmed();
            }
            else
            {
                // 没有歌手名字
                musicName = fileName.mid(0,fileName.indexOf('.')).trimmed();
            }
        }
        // 作者为空
        if(musicSinger.isEmpty())
        {
            if(index != -1)
            {
                musicSinger = fileName.mid(index+1,fileName.indexOf('.') - index - 1);
            }
            else
            {
                musicSinger = "未知歌手";
            }
        }
        if(musicAlbumn.isEmpty())
        {
            musicAlbumn = "未知专辑";
        }

        qDebug()<<musicName<<musicSinger<<musicAlbumn<<duration;
    }
}
